<!--
 * @Author: wanglianxin
 * @Date: 2022-09-17 15:24:14
 * @LastEditors: wanglianxin
 * @LastEditTime: 2022-09-17 15:42:09
 * @FilePath: \standard-check\src\components\Authorization\Fingerprint.vue
 * @Description: 指纹api
-->
<script>
export default {
  data() {
    return {
      fingerHost: '', //指纹host
      fingerSocket: '', //指纹socket
      callback: {
        GetPort: [],
        GetTemplet: [],
        GetFeature: [],
        Match: [],
        GetImageData: [],
        FusionTemplate: [],
      },
    };
  },
  methods: {
    /**
     * @description: 初始化参数
     * @param {*} url ws的url
     * @return {*}
     */
    initParam: function (url) {
      // 参数赋值
      this.fingerHost = url;
    },
    /**
     * @description: 连接
     * @param {*} onSucc
     * @param {*} onFail
     * @return {*}
     */
    connect: function (onSucc, onFail) {
      if (typeof this.fingerSocket == 'object' && this.fingerSocket != null) return; // 防止重复创建socket
      var that = this;
      onSucc =
        onSucc ||
        function () {
          console.log('socket连接成功');
        };
      onFail =
        onFail ||
        function () {
          console.log('socket连接失败');
        };

      // 创建websocket

      this.fingerSocket = new WebSocket(this.fingerHost);

      // CONNECTING:0
      // OPEN:1
      // CLOSING:2
      // CLOSED:3
      this.fingerSocket.onopen = (msg) => {
        if (this.fingerSocket.readyState == WebSocket.OPEN) {
          if (onSucc) {
            onSucc();
          }
        } else {
          if (onFail) onFail();
        }
      };
      this.fingerSocket.onerror = () => {
        if (onFail) onFail();
      };
      this.fingerSocket.onclose = (msg) => {
        //console.log("onclose:"+msg);
      };
      this.fingerSocket.onFailor = (msg) => {
        onFail(10001, '连接失败');
      };
      this.fingerSocket.onmessage = (msg) => {
        console.log(203, 'msg', msg);
        if (typeof msg.data != 'string') return;
        var arrayMsg = msg.data.split('$&');
        //console.log("length="+arrayMsg.length);
        //console.log(msg.data);
        if (arrayMsg.length < 2) return;
        var cmd = arrayMsg[0];
        var ret = arrayMsg[1];
        switch (cmd) {
          case '105': // 获取端口号
            if (ret == '0') {
              that.callback['GetPort'][0](arrayMsg[2]);
            } else {
              that.callback['GetPort'][1](ret, this.GetErrMsg(ret));
            }
            break;
          case '100': // 获取模板
            if (ret == '0') {
              that.callback['GetTemplet'][0](arrayMsg[2]);
            } else {
              that.callback['GetTemplet'][1](ret, this.GetErrMsg(ret));
            }
            break;

          case '101': // 获取特征
            if (ret == '0') {
              that.callback['GetFeature'][0](arrayMsg[2]);
            } else {
              that.callback['GetFeature'][1](ret, this.GetErrMsg(ret));
            }
            break;

          case '102': // 特征比对
            if (ret == '0') {
              // 比对成功
              that.callback['Match'][0]();
            } else {
              that.callback['Match'][1](ret, this.GetErrMsg(ret));
            }
            break;
          case '103': // 获取图像
            if (ret == '0') {
              that.callback['GetImageData'][0](arrayMsg[2]);
            } else {
              that.callback['GetImageData'][1](ret, this.GetErrMsg(ret));
            }
            break;

          case '104': // 融合蒙版
            if (ret == '0') {
              that.callback['FusionTemplate'][0](arrayMsg[2]);
            } else {
              that.callback['FusionTemplate'][1](ret, this.GetErrMsg(ret));
            }
            break;

          //-----------------------------------------------
          case '120': // 获取模板
            if (ret == '0') {
              that.callbackGA['GetTemplet'][0](arrayMsg[2]);
            } else {
              that.callbackGA['GetTemplet'][1](ret, this.GetErrMsgGA(ret));
            }
            break;

          case '121': // 获取特征
            if (ret == '0') {
              that.callbackGA['GetFeature'][0](arrayMsg[2]);
            } else {
              that.callbackGA['GetFeature'][1](ret, this.GetErrMsgGA(ret));
            }
            break;

          case '122': // 特征比对
            if (ret == '0') {
              // 比对成功
              that.callbackGA['Match'][0]();
            } else {
              that.callbackGA['Match'][1](ret, this.GetErrMsgGA(ret));
            }
            break;

          case '123': // 获取图像
            if (ret == '0') {
              that.callbackGA['GetImageData'][0](arrayMsg[2]);
            } else {
              that.callbackGA['GetImageData'][1](ret, this.GetErrMsgGA(ret));
            }
            break;

          case '1000':
            that.callbackGA['GetTemplet'][2](ret);
            break;
        }
      };
    },
    /**
     * @description: 断开连接
     * @return {*}
     */
    disconnect: function () {
      if (typeof this.fingerSocket == 'object' && this.fingerSocket != null) {
        this.fingerSocket.onclose = function () {};
        this.fingerSocket.close();
        this.fingerSocket = null;
      }
      //onSucc();
    },
    // 获取指纹特征
    FPIGetFeature(port, timeout, onSucc, onFail) {
      onSucc =
        onSucc ||
        function () {
          console.log('获取特征成功');
        };
      onFail =
        onFail ||
        function (msg) {
          console.log('获取特征失败:' + msg);
        };
      this.callback['GetFeature'][0] = onSucc;
      this.callback['GetFeature'][1] = onFail;
      this.sendMsg('101$&' + port + '$&' + timeout);
    },
    /**
     * @description: 发送ws请求
     * @param {*} msg
     * @return {*}
     */
    sendMsg(msg) {
      if (this.fingerSocket) this.fingerSocket.send(msg);
      else alert('socket连接失败，可能connet未调用。');
    },
    /**
     * @description: 转换错误码为文字
     * @param {*} ret 错误码
     * @return {*}
     */
    GetErrMsg(ret) {
      switch (ret) {
        case '0':
          return '成功';
          break;

        case '-1':
          return '失败';
          break;

        case '-2':
          return '校验错误';
          break;

        case '-3':
          return '参数错误';
          break;

        case '-4':
          return '设备内没指纹信息';
          break;

        case '-5':
          return '手指未按或手指质量差';
          break;

        case '-6':
          return '指纹合成失败';
          break;

        case '-7':
          return '指纹比对失败';
          break;

        case '-8':
          return '内存不足';
          break;

        case '-9':
          return '有闪存错';
          break;

        case '-10':
          return '传感器错';
          break;

        case '-11':
          return '请抬起手';
          break;

        case '-12':
          return '不支持的指令';
          break;

        case '-13':
          return '操作超时';
          break;

        case '-14':
          return '设备占用';
          break;

        case '-15':
          return '设备断开';
          break;

        case '-16':
          return '特点过少';
          break;

        case '-17':
          return '取消操作';
          break;

        case '-18':
          return '文件错误';
          break;

        default:
          return '';
          break;
      }
    },
    GetErrMsgGA(ret) {
      switch (ret) {
        case '1':
        case '0':
          return '成功';
          break;

        case '-1':
          return '参数错误';
          break;

        case '-2':
          return '内存不足';
          break;

        case '-3':
          return '不支持令';
          break;

        case '-4':
          return '设备断开';
          break;

        case '-5':
          return '未初始化';
          break;

        case '-6':
          return '错误号错';
          break;

        case '-9':
          return '其他错误';
          break;

        case '-13':
          return '操作超时';
          break;

        default:
          return '';
          break;
      }
    },
  },
};
</script>
